package com.jiayang.hhotel.base;

import android.os.Bundle;
import android.view.View;

import com.jiayang.hhotel.R;
import com.jiayang.hhotel.view.StateView;

import cn.droidlover.xdroidmvp.base.SimpleRecAdapter;
import cn.droidlover.xdroidmvp.mvp.XLazyFragment;
import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xrecyclerview.XRecyclerContentLayout;
import cn.droidlover.xrecyclerview.XRecyclerView;

/**
 * Created by Administrator on 2017/5/19 0019.
 */

public abstract class BasePagerFragment extends XLazyFragment {
    private XRecyclerContentLayout contentLayout;
    StateView errorView;
    @Override
    public void initData(Bundle savedInstanceState) {
        contentLayout=(XRecyclerContentLayout)getRootView().findViewById(R.id.contentLayout);
        initAdapter();
    }
    private void  initAdapter(){

        setLayoutManager(contentLayout.getRecyclerView());
        contentLayout.getRecyclerView()
                .setAdapter(getAdapter());
        contentLayout.getRecyclerView()
                .setOnRefreshAndLoadMoreListener(new XRecyclerView.OnRefreshAndLoadMoreListener() {
                    @Override
                    public void onRefresh() {
                        //getP().loadData(getType(), 1);
                    }

                    @Override
                    public void onLoadMore(int page) {
                        //getP().loadData(getType(), page);
                    }
                });
        if (errorView == null) {
            errorView = new StateView(context);
        }
        contentLayout.errorView(errorView);
        contentLayout.loadingView(View.inflate(getContext(), R.layout.view_loading, null));

        contentLayout.getRecyclerView().useDefLoadMoreView();
    }
//    public void showData(int page, GankResults model) {
//        if (page > 1) {
//            getAdapter().addData(model.getResults());
//        } else {
//            getAdapter().setData(model.getResults());
//        }
//
//        contentLayout.getRecyclerView().setPage(page, MAX_PAGE);
//
//        if (getAdapter().getItemCount() < 1) {
//            contentLayout.showEmpty();
//            return;
//        }
//    }
    @Override
    public int getLayoutId() {
        return R.layout.fragment_base_pager;
    }
    public abstract SimpleRecAdapter getAdapter();

    public abstract void setLayoutManager(XRecyclerView recyclerView);

    public abstract String getType();
    @Override
    public Object newP() {
        return null;
    }
    public void showError(NetError error) {
        if (error != null) {
            switch (error.getType()) {
                case NetError.ParseError:
                    errorView.setMsg("数据解析异常");
                    break;

                case NetError.AuthError:
                    errorView.setMsg("身份验证异常");
                    break;

                case NetError.BusinessError:
                    errorView.setMsg("业务异常");
                    break;

                case NetError.NoConnectError:
                    errorView.setMsg("网络无连接");
                    break;

                case NetError.NoDataError:
                    errorView.setMsg("数据为空");
                    break;

                case NetError.OtherError:
                    errorView.setMsg("其他异常");
                    break;
            }
            contentLayout.showError();
        }
    }
}
